/*  
 * RTFCreate.java  
 *  
 * Created on 2007年8月9日, 上午9:32  
 *  
 * To change this template, choose Tools | Template Manager  
 * and open the template in the editor.  
 */

package test;

import java.awt.Color;
import java.io.FileOutputStream;

import com.lowagie.text.Cell;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;

/**
 * 
 * @author julycn
 */
public class RTFCreate {

	/**
	 * Creates a new instance of RTFCreate
	 */
	public RTFCreate() {
	}

	public static void main(String[] args) {
		for (int i = 1; i <= 1; i ++) {
			final int j = i;
			Runnable r = new Runnable() {
				public void run() {
					RTFCreate rtfCreate = new RTFCreate();
					try {
						rtfCreate.createTableRTF("d:\\工单-"+j+".rtf");
					} catch (Exception ex) {
						ex.printStackTrace();
					}
				}
			};
			Thread t = new Thread(r);
			t.start();
		}
	}
	
	
	public void createFaceRTF() throws Exception {
		// 创建word文档
		//Document document = new Document(PageSize.A4);
	}
	
	public void createTableRTF(String file) throws Exception {
		// 创建word文档
		Document document = new Document(PageSize.A4, 90, 90, 70, 70);
		// 输入word文档
		RtfWriter2.getInstance(document, new FileOutputStream(file));
		//文档打开
		document.open();
		
		// 中文字体
		BaseFont bfChinese = 
			BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		
		//章节一
        Chapter chapter1 = new Chapter(1);
        chapter1.setNumberDepth(0);
		
		//面页标题
        Paragraph p = new Paragraph("WLYX/F/JL/B/ZY-005-2004", new Font(bfChinese, 12, Font.NORMAL));
        p.setIndentationLeft(-30);
        p.setSpacingAfter(15);
        chapter1.add(p);
		
		p = new Paragraph("交换修改数据任务单(电子版)", new Font(bfChinese, 14, Font.BOLD));
		p.setAlignment(Element.ALIGN_CENTER);
		p.setSpacingAfter(15);
		chapter1.add(p);
		
		p = new Paragraph("网话编号:09-1674", new Font(bfChinese, 11, Font.BOLD));
		p.setAlignment(Element.ALIGN_RIGHT);
		chapter1.add(p);
		
		//面页表格
		Table t = new Table(8);
		t.setOffset(0);
		t.setAlignment(Element.ALIGN_CENTER);
		t.setWidth(130);
		t.setBorderWidth(1);
		t.setPadding(5);
		
		//面页表格内容
		//第一行
		Cell c = new Cell(new Paragraph("工程\n名称", new Font(bfChinese, 11, Font.BOLD)));
		c.setHorizontalAlignment(Element.ALIGN_CENTER);
		t.addCell(c);
		c = new Cell(new Paragraph("拆除MSC5、7至联通IGW1、2的光路", new Font(bfChinese, 11, Font.BOLD)));
		c.setColspan(7);
		c.setVerticalAlignment(Element.ALIGN_MIDDLE);
		t.addCell(c);
		
		
		//第二行
		String s = "\n\n" +
					"    根据客响中心《C网优化腾空资源建议》及网络部回复，请拆除MSC5、MSC7原连接至联通IGW1、IGW2的光口模块，拆除后的MSC侧光口将用于C网基站之用。\n" +
					"    具体内容详见附页。\n" +
					"    本次工程由移动建设部负责施工，联系人：罗荣，联系电话：63630406。\n";
		c = new Cell(new Paragraph("\n\n\n\n\n\n\n\n\n\n主\n\n要\n\n内\n\n容\n\n与\n\n要\n\n求\n\n\n\n\n\n\n\n\n\n", new Font(bfChinese, 11, Font.BOLD)));
		c.setHorizontalAlignment(Element.ALIGN_CENTER);
		t.addCell(c);
		p = new Paragraph(s, new Font(bfChinese, 12, Font.NORMAL));
		p.setLeading(25);
		c = new Cell(p);
		c.setColspan(7);
		t.addCell(c);
		
		//第三行
		c = new Cell(new Paragraph("备注", new Font(bfChinese, 11, Font.BOLD)));
		c.setHorizontalAlignment(Element.ALIGN_CENTER);
		t.addCell(c);
		c = new Cell(new Paragraph("", new Font(bfChinese, 11, Font.NORMAL)));
		c.setColspan(7);
		t.addCell(c);
		
		//第四行
		c = new Cell(new Paragraph("完成日期", new Font(bfChinese, 11, Font.BOLD)));
		c.setHorizontalAlignment(Element.ALIGN_CENTER);
		t.addCell(c);
		c = new Cell(new Paragraph("至2009-5-22", new Font(bfChinese, 11, Font.NORMAL)));
		t.addCell(c);
		c = new Cell(new Paragraph("传输编号", new Font(bfChinese, 11, Font.BOLD)));
		c.setHorizontalAlignment(Element.ALIGN_CENTER);
		t.addCell(c);
		c = new Cell(new Paragraph("09-1242", new Font(bfChinese, 11, Font.NORMAL)));
		t.addCell(c);
		c = new Cell(new Paragraph("联系人", new Font(bfChinese, 11, Font.BOLD)));
		c.setHorizontalAlignment(Element.ALIGN_CENTER);
		t.addCell(c);
		c = new Cell(new Paragraph("程静", new Font(bfChinese, 11, Font.NORMAL)));
		t.addCell(c);
		c = new Cell(new Paragraph("联系电话", new Font(bfChinese, 11, Font.BOLD)));
		c.setHorizontalAlignment(Element.ALIGN_CENTER);
		t.addCell(c);
		c = new Cell(new Paragraph("52128880", new Font(bfChinese, 11, Font.NORMAL)));
		t.addCell(c);
		
		//第五行
		c = new Cell(new Paragraph("设计人", new Font(bfChinese, 11, Font.BOLD)));
		c.setHorizontalAlignment(Element.ALIGN_CENTER);
		t.addCell(c);
		c = new Cell(new Paragraph("潘  江", new Font(bfChinese, 11, Font.NORMAL)));
		t.addCell(c);
		c = new Cell(new Paragraph("设计日期", new Font(bfChinese, 11, Font.BOLD)));
		c.setHorizontalAlignment(Element.ALIGN_CENTER);
		t.addCell(c);
		c = new Cell(new Paragraph("2009-5-13", new Font(bfChinese, 11, Font.NORMAL)));
		t.addCell(c);
		c = new Cell(new Paragraph("交换室审核", new Font(bfChinese, 11, Font.BOLD)));
		c.setHorizontalAlignment(Element.ALIGN_CENTER);
		t.addCell(c);
		c = new Cell(new Paragraph("沈丽中", new Font(bfChinese, 11, Font.NORMAL)));
		t.addCell(c);
		c = new Cell(new Paragraph("审核日期", new Font(bfChinese, 11, Font.BOLD)));
		c.setHorizontalAlignment(Element.ALIGN_CENTER);
		t.addCell(c);
		c = new Cell(new Paragraph("2009-05-13", new Font(bfChinese, 11, Font.NORMAL)));
		t.addCell(c);
		
		//第六行
		c = new Cell(new Paragraph("任务来源", new Font(bfChinese, 11, Font.BOLD)));
		c.setHorizontalAlignment(Element.ALIGN_CENTER);
		t.addCell(c);
		c = new Cell(new Paragraph("网络部", new Font(bfChinese, 11, Font.NORMAL)));
		t.addCell(c);
		c = new Cell(new Paragraph("", new Font(bfChinese, 11, Font.BOLD)));
		c.setHorizontalAlignment(Element.ALIGN_CENTER);
		t.addCell(c);
		c = new Cell(new Paragraph("", new Font(bfChinese, 11, Font.NORMAL)));
		t.addCell(c);
		c = new Cell(new Paragraph("调度部审核", new Font(bfChinese, 11, Font.BOLD)));
		c.setHorizontalAlignment(Element.ALIGN_CENTER);
		t.addCell(c);
		c = new Cell(new Paragraph("何泰山", new Font(bfChinese, 11, Font.NORMAL)));
		t.addCell(c);
		c = new Cell(new Paragraph("审核日期", new Font(bfChinese, 11, Font.BOLD)));
		c.setHorizontalAlignment(Element.ALIGN_CENTER);
		t.addCell(c);
		c = new Cell(new Paragraph("2009-05-13", new Font(bfChinese, 11, Font.NORMAL)));
		t.addCell(c);
		
		//第七行
		c = new Cell(new Paragraph("抄送单位", new Font(bfChinese, 11, Font.BOLD)));
		c.setHorizontalAlignment(Element.ALIGN_CENTER);
		t.addCell(c);
		c = new Cell(new Paragraph("网络部交换处,长途无线部,监管事务处", new Font(bfChinese, 11, Font.NORMAL)));
		c.setColspan(7);
		t.addCell(c);
		
		chapter1.add(t);
		document.add(chapter1);
		
		
		//章节二
		document.setPageSize(PageSize.A4.rotate());
		document.setMargins(90, 90, 70, 70);
        Chapter chapter2 = new Chapter(2);
        chapter2.setNumberDepth(0);
		
		//工单正文标题
        chapter2.add(new Paragraph("一、中继\n", new Font(bfChinese, 13, Font.BOLD)));
        chapter2.add(new Paragraph("1. 拆除\n", new Font(bfChinese, 11, Font.BOLD)));
		
		//打印表格
		Table table = new Table(10);
		table.setOffset(0);
		table.setWidth(100);
		table.setBorderWidth(1);
		table.setCellsFitPage(true);
		
		//添加表头元素
		String[] headerStrings = {"序号", "A局号", "A局名", "DTM", "BW/CH", "系统号", "B局名", "B局号", "DTM", "说明"};
		for (String h : headerStrings) {
			Cell cell = new Cell(new Paragraph(h, new Font(bfChinese, 11, Font.BOLD)));
			cell.setHorizontalAlignment(Element.ALIGN_CENTER);
			cell.setHeader(true);
			table.addCell(cell);
		}
		table.endHeaders();
		// 表头结束
		
		//制作表体
		String[] bodyStrings = {"1", "西安C网", "MSC5DCS", "07-0-6-1-1-1-1", "683-713/1-31", "023", "联通", "IGW1", "07-15-08-00", "N7 TUP"};
		for (int i = 0; i < 100; i ++) {
			bodyStrings[0] = String.valueOf((i+1));
			for (String h : bodyStrings) {
				Cell cell = new Cell(new Paragraph(h, new Font(bfChinese, 10, Font.NORMAL)));
				cell.setHorizontalAlignment(Element.ALIGN_CENTER);
				table.addCell(cell);
			}		
		}
		chapter2.add(table);
		document.add(chapter2);
		
		document.close();

	}

	public void createRTF() throws Exception {
		// 创建word文档
		Document document = new Document(PageSize.A4);
		// 输入word文档
		RtfWriter2.getInstance(document, new FileOutputStream("d:\\word.rtf"));
		document.open();
		// 中文字体
		BaseFont bfChinese = 
			BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		Font fontChinese = new Font(bfChinese, 12, Font.HELVETICA);
		
		// 创建有3列的表格
		Table table = new Table(3);
		document.add(new Paragraph("生成rft文档！", fontChinese));
		table.setBorderWidth(1);
		table.setBorderColor(new Color(0, 0, 255));
		table.setPadding(5);
		table.setSpacing(5);

		// 添加表头元素
		Cell cell = new Cell("header");
		cell.setHeader(true);
		cell.setColspan(3);
		table.addCell(cell);
		table.endHeaders();// 表头结束

		// 表格主体
		cell = new Cell("Example cell with colspan 1 and rowspan 2");
		cell.setRowspan(2);
		cell.setBorderColor(new Color(255, 0, 0));
		table.addCell(cell);
		table.addCell("1.1");
		table.addCell("2.1");
		table.addCell("1.2");
		table.addCell("2.2");
		table.addCell(new Paragraph("测试1", fontChinese));
		table.addCell("big cell");
		cell.setRowspan(2);
		cell.setColspan(2);
		table.addCell(cell);
		table.addCell(new Paragraph("测试2", fontChinese));
		document.add(table);
		// 在表格末尾添加图片
		Image png = Image.getInstance("d:\\200712111917392406.jpg");
		document.add(png);
		document.close();
	}
}